<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('库存明细')" />
</head>
<body class="gray-bg">
     <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="formId">
                    <div class="select-list">
                        <ul>
                            <li shiro:lacksRole="DragonWarehouseAdministrator">
                                <label>仓库编号：</label>
                                <input type="text" id="wId" name="wId" placeholder="全名"/>
                            </li>
                            <li shiro:lacksRole="DragonWarehouseAdministrator">
                                <label>仓库名称：</label>
                                <input type="text" id="wName" name="wName" placeholder="全名"/>
                            </li>
                            <li>
                                <label>水果种类编号：</label>
                                <input type="text" id="fruittypesId" name="fruittypesId" placeholder="全名"/>
                            </li>
                            <li>
                                <label>水果种类名称：</label>
                                <input type="text" id="fruittypesName" name="fruittypesName" placeholder="全名"/>
                            </li>
                            <li>
                                <label>水果编号：</label>
                                <input type="text" name="fruitId" placeholder="全名"/>
                            </li>
                            <li>
                                <label>水果名称：</label>
                                <input type="text" name="fruitName"/>
                            </li>
                            <li>
                                <label>进价(元/500g)：</label>
                                <input type="text" name="jinjia"/>
                            </li>
                            <li>
                                <label>状态：</label>
                                <select name="isStatus" th:with="type=${@dict.getType('fruits')}">
                                    <option value="">所有</option>
                                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                                </select>
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>
            <div class="btn-group-sm" id="toolbar" role="group">
                <a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="inventory:warehousestock:add">
                    <i class="fa fa-plus"></i> 添加仓库库存
                </a>
                <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()" shiro:hasAnyRoles="admin, WarehouseManager" shiro:hasPermission="inventory:warehousestock:remove">
                    <i class="fa fa-remove"></i> 批量清零
                </a>
            </div>
            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var editFlag = [[${@permission.hasPermi('inventory:warehousestock:edit')}]];
        var isStatusDatas = [[${@dict.getType('fruits')}]];
        var prefix = ctx + "inventory/warehousestock";

        $(function() {
            var options = {
                url: prefix + "/list",
                createUrl: prefix + "/add",
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove",
                exportUrl: prefix + "/export",
                modalName: "库存明细",
                columns: [
                    {
                    checkbox: true
                },
                {
                    field: 'whId',
                    title: '序号',
                    formatter: function (value, row, index) {
                        return index + 1
                    }
                },
                {
                    field: 'wId',
                    title: '仓库编号',
                    visible: false
                },
                {
                    title: '仓库名称',
                    formatter: function(value, row, index) {
                        (function () {
                            selectWareHouseNameByWareHouseId(row.wId, index)
                        })();
                        return ('<span id=' + "wareHouseNameSpan" + index + '></span>')
                    }
                },
                {
                    field: 'fruittypesId',
                    title: '水果种类编号',
                    visible: false
                },
                {
                    title: '水果种类名称',
                    formatter: function(value, row, index) {
                        (function () {
                            selectFruitTypeNameByFruitTypeId(row.fruittypesId, index)
                        })();
                        return ('<span id=' + "fruitTypeNameSpan" + index + '></span>')
                    }
                },
                {
                    field: 'fruitId',
                    title: '水果编号',
                    visible: false
                },
                {
                    field: 'fruitName',
                    title: '水果名称'
                },
                {
                    field: 'jinjia',
                    title: '进价(元/500g)',
                    formatter: function (value) {
                        return parseFloat(value).toFixed(2);
                    }
                },
                {
                    field: 'whCount',
                    title: '库存(kg)',
                    formatter: function(value, row, index) {
                        return parseFloat(value).toFixed(2);
                    }
                },
                {
                    field: 'createdBy',
                    title: '创建人'
                },
                {
                    field: 'createdTime',
                    title: '创建时间'
                },
                {
                    field: 'updatedBy',
                    title: '更新人'
                },
                {
                    field: 'updatedTime',
                    title: '更新时间'
                },
                {
                    field: 'isStatus',
                    title: '状态',
                    formatter: function(value, row, index) {
                        if (value === 1) {
                            return ('<i class="fa fa-toggle-on text-info fa-2x" onclick="warehousestockBan(' + row.whId + ',' + 0 + ')"></i>')
                        } else {
                            return ('<i class="fa fa-toggle-off text-info fa-2x" onclick="warehousestockBan(' + row.whId + ',' + 1 + ')"></i>')
                        }
                    }
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.whId + '\')"><i class="fa fa-edit"></i>修改库存</a> ');
                        var role = [[${@permission.hasAnyRoles('admin, WarehouseManager')}]]; // 验证用户是否是管理员 或者 仓库经理
                        actions.push('<a class="btn btn-danger btn-xs ' + role + '" onclick="confirmwhCountClear(' + row.whId + ') "><i class="fa fa-remove"></i>清零</a>');
                        return actions.join('');
                    }
                }]
            };
            $.table.init(options);
        });
    </script>

     <!--  根据水果种类名称查询水果种类编号  -->
     <script>
         $(document).ready(function () {
             $("#fruittypesName").on('input', function () {
                 var fruittypesName = $(this).val();
                 if (fruittypesName) {
                     selectFruitTypeIdByFruitTypeName(fruittypesName);
                 } else {
                     $("#fruittypesId").val(""); // 如果输入框为空就清空水果种类编号输入框
                 }
             })
         })
         function selectFruitTypeIdByFruitTypeName(fruittypesName) {
             $.ajax({
                 url: "/inventory/fruits/selectFruitTypeId",
                 dataType: "json",
                 data: {
                     "name": fruittypesName
                 },
                 type: "post",
                 success: function (resp) {
                     if (resp.fruitTypeId != null && resp.fruitTypeId !== "") {
                         $("#fruittypesId").val(resp.fruitTypeId);
                     } else {
                         $("#fruittypesId").val(""); // 如果查不到就清空水果种类编号输入框
                     }
                 },
                 error:function(xhr,type,errorThrown){
                     console.log('XMLHttpRequest 对象:',xhr);
                     console.log('错误类型:', type);
                     console.log('错误描述:', errorThrown);
                     $.modal.alertError("请求失败");
                 }
             })
         }
     </script>

     <!--  根据水果种类编号查询水果种类名称  -->
     <script>
        // 监测水果种类id输入框
         $(document).ready(function () {
             $("#fruittypesId").on('input', function () {
                 var fruittypesId = $(this).val();
                 if (fruittypesId) {
                     selectFruitTypeNameByFruitTypeId(fruittypesId);
                 } else {
                     $("#fruittypesName").val(""); // 如果输入框为空就清空水果种类编号输入框
                 }
             })
         })

        // 根据水果种类编号查询水果种类名称
        function selectFruitTypeNameByFruitTypeId(fruittypesId, index) {
            $.ajax({
                url: "/inventory/warehousestock/selectFruitTypeName",
                dataType: "json",
                data: {
                    "code": fruittypesId
                },
                type: "post",
                success: function (resp) {
                    if (index != null) {
                        $("#fruitTypeNameSpan" + index).html(resp.fruitTypeName);
                    } else {
                        if (resp.fruitTypeName != null && resp.fruitTypeName !== "") {
                            $("#fruittypesName").val(resp.fruitTypeName);
                        } else {
                            $("#fruittypesName").val(""); // 如果查不到就清空水果种类名称输入框
                        }
                    }
                },
                error:function(xhr,type,errorThrown){
                    console.log('XMLHttpRequest 对象:',xhr);
                    console.log('错误类型:', type);
                    console.log('错误描述:', errorThrown);
                    $.modal.alertError("请求失败");
                }
            })
        }
     </script>

<!--  根据仓库名称查询仓库编号  -->
        <script>
            // 监测仓库名称输入框
            $(document).ready(function () {
                $("#wName").on('input', function () {
                    var wName = $(this).val();
                    if (wName) {
                        selectWareHouseIdByWareHouseName(wName);
                    } else {
                        $("#wId").val(""); // 如果输入框为空就清空仓库编号输入框
                    }
                })
            })
            // 根据仓库名称查询仓库编号
            function selectWareHouseIdByWareHouseName(wName, index) {
                $.ajax({
                    url: "/inventory/warehousestock/selectWareHouseId",
                    dataType: "json",
                    data: {
                        "wName": wName
                    },
                    type: "post",
                    success: function (resp) {
                        if (resp.wareHouseId != null && resp.wareHouseId !== "") {
                            $("#wId").val(resp.wareHouseId);
                        } else {
                            $("#wId").val(""); // 如果查不到就清空仓库编号输入框
                        }
                    },
                    error:function(xhr,type,errorThrown){
                        console.log('XMLHttpRequest 对象:',xhr);
                        console.log('错误类型:', type);
                        console.log('错误描述:', errorThrown);
                        $.modal.alertError("请求失败");
                    }
                })
            }

        </script>

<!--  根据仓库编号查询仓库名称   -->
     <script>
         // 监测仓库名称输入框
         $(document).ready(function () {
             $("#wId").on('input', function () {
                 var wId = $(this).val();
                 if (wId) {
                     selectWareHouseNameByWareHouseId(wId);
                 } else {
                     $("#wName").val(""); // 如果输入框为空就清空仓库名称输入框
                 }
             })
         })
         // 根据仓库编号查询仓库名称
         function selectWareHouseNameByWareHouseId(wId, index) {
             $.ajax({
                 url: "/inventory/warehousestock/selectWareHouseName",
                 dataType: "json",
                 data: {
                     "wNumber": wId
                 },
                 type: "post",
                 success: function (resp) {
                     if (index != null) {
                         $("#wareHouseNameSpan" + index).html(resp.wareHouseName);
                     } else {
                         if (resp.wareHouseName != null && resp.wareHouseName !== "") {
                             $("#wName").val(resp.wareHouseName);
                         } else {
                             $("#wName").val(""); // 如果查不到就清空仓库名称输入框
                         }
                     }
                 },
                 error:function(xhr,type,errorThrown){
                     console.log('XMLHttpRequest 对象:',xhr);
                     console.log('错误类型:', type);
                     console.log('错误描述:', errorThrown);
                     $.modal.alertError("请求失败");
                 }
             })
         }

     </script>

<!--  下架水果  -->
    <script>
        // 下架水果
        function warehousestockBan(whId, isStatus) {
            let msgConfirm;
            if (isStatus === 0) {
                msgConfirm = "确定要下架该仓库的该种水果吗?";
            } else {
                msgConfirm = "确定要上架该仓库的该种水果吗?"
            }
            $.modal.confirm(msgConfirm, function() {
                $.modal.loading("正在处理中，请稍后...");
                setTimeout(function() {
                    warehousestockBans(whId, isStatus);
                }, 1000); // 3000毫秒等于3秒
            });
        }
        function warehousestockBans(whId, isStatus) {
            $.ajax({
                url: "/inventory/warehousestock/edit",
                data: {
                    "whId": whId,
                    "isStatus": isStatus
                },
                dataType: "json",
                type: "post",
                success: function (resp) {
                    $.modal.closeLoading(); // 关闭遮罩层
                    $.modal.msgSuccess(resp.msg);
                    location.reload();
                },
                error:function(xhr,type,errorThrown){
                    console.log('XMLHttpRequest 对象:',xhr);
                    console.log('错误类型:', type);
                    console.log('错误描述:', errorThrown);
                    $.modal.alertError("请求失败");
                }
            })
        }
    </script>
<!--  清空库存  -->
     <script>
        // 清空库存
        function confirmwhCountClear(whId) {
            $.modal.confirm('确定要清空库存吗?', function() {
                $.modal.loading("正在处理中，请稍后..."); // 打开遮罩层
                setTimeout(function() {
                    whCountClearWhId(whId);
                }, 1500); // 3000毫秒等于3秒
            });
        }
        function whCountClearWhId(whId) {
            $.ajax({
                url: "/inventory/warehousestock/whCountClear",
                data: {
                    "whId": whId
                },
                dataType: "json",
                type: "post",
                success: function (resp) {
                    console.log("库存清空后的参数:")
                    console.log(resp);
                    $.modal.closeLoading(); // 关闭遮罩层
                    $.modal.msgSuccess(resp.msg);
                    location.reload();
                },
                error:function(xhr,type,errorThrown){
                    console.log('XMLHttpRequest 对象:',xhr);
                    console.log('错误类型:', type);
                    console.log('错误描述:', errorThrown);
                    $.modal.alertError("请求失败");
                }
            })
        }
    </script>
</body>
</html>